package com.dingtalk.common;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.*;
import java.nio.Buffer;
import java.util.Properties;

/**
 * 企业应用接入时的常量定义
 */
public class Env {

    public static final String FILENAME = "sdoa.properties";
    /**
     * 企业应用接入秘钥相关
     */
    public static String CORP_ID = null;
    public static String CORP_SECRET = null;
    public static String SSO_Secret = null;
    /**
     * DING API地址
     */
	public static String OAPI_HOST = null;
    /**
     * 企业应用后台地址，用户管理后台免登使用
     *
     * https://oa.dingtalk.com/?code=4f498d9abd803c3ebb23c5087d86fb71#/welcome
     */
	public static String OA_BACKGROUND_URL = null;
    /**
     * 企业通讯回调加密Token，注册事件回调接口时需要传递给钉钉服务器
     */
	public static String TOKEN = null;
	public static String ENCODING_AES_KEY = null;

    static{
        //FileInputStream in = null;
        //InputStreamReader reader = null;
        BufferedReader br = null;
        try{
            Resource resource = new ClassPathResource(FILENAME);
            Properties properties = new Properties();
            InputStream is = resource.getInputStream();
            //reader = new InputStreamReader(in,"UTF-8");
            br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            properties.load(br);
            CORP_ID = properties.getProperty("CORP_ID");
            CORP_SECRET = properties.getProperty("CORP_SECRET");
            SSO_Secret = properties.getProperty("SSO_Secret");
            OAPI_HOST = properties.getProperty("OAPI_HOST");
            OA_BACKGROUND_URL = properties.getProperty("OA_BACKGROUND_URL");
            TOKEN = properties.getProperty("TOKEN");
            ENCODING_AES_KEY = properties.getProperty("ENCODING_AES_KEY");
            System.out.println("读取配置信息成功！");
            showConfig();
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("读取配置信息失败！");
        }finally{
            if(br != null){
                try{
                    br.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

    public static void showConfig(){
        System.out.println("-----------------------配置信息-----------------");
        System.out.println("CORP_ID: "+CORP_ID);
        System.out.println("CORP_SECRET: "+CORP_SECRET);
        System.out.println("SSO_Secret: "+SSO_Secret);
        System.out.println("OAPI_HOST: "+OAPI_HOST);
        System.out.println("OA_BACKGROUND_URL: "+OA_BACKGROUND_URL);
        System.out.println("TOKEN: "+TOKEN);
        System.out.println("ENCODING_AES_KEY: "+ENCODING_AES_KEY);
        System.out.println("----------------------------------------------");
    }


}
